<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>四年级暑假数学作业</title>
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/vue/2.2.4/vue.min.js"></script>
<style>
body{
    font-size:16px;
}
.t-item{
    padding-bottom:20vh;
    height:50vw;
}
.center{
    text-align: center;
}
.mb30{
    padding-bottom:30px;
}
.mr5{
    margin-right:5px;
}
.fuhao{
    font-weight: bold;
    margin:0 2px;
}
.flex{
    display: flex;
    align-items: center;
    margin-bottom:8px;
}
.flex1{
    flex:1;
}
input.form-control{
    width:auto;
}
.modal{
    display: block;
    overflow: auto;
    background:white;
}
.col-xs-4{
    padding:0 5px;
}
</style>
</head>
<body>
<div class="container" id="app">
    <h3 class="center">四年级暑假数学作业</h3>
    <p class="mb30"></p>
    <div class="row">
        <div class="col-xs-6 t-item" v-for="item in list">
            <span v-for="(v,i) in item.exp" :class="{fuhao:is_sign(v)}">{{v}}</span>
            <br v-if="item.exp.length>3">
            =
            <span v-show="show">{{item.r}}</span>
        </div>
    </div>
    <p class="mb30 row">
        <div :class="cache_list.length>0?'col-xs-4':'col-xs-6'">
            <button class="btn btn-success btn-block" type="button" @click="init">再次生成</button>
        </div>
        <div :class="cache_list.length>0?'col-xs-4':'col-xs-6'">
            <button class="btn btn-default btn-block" @click="show=!show">{{show?'隐藏':'显示'}}答案</button>
        </div>
        <div class="col-xs-4" v-if="cache_list.length>0">
            <button class="btn btn-info btn-block" @click="show_modal=true">查看历史({{cache_list.length}})</button>
        </div>
    </p>

    <div class="modal" v-show="show_modal">
        <h3 class="center">历史记录</h3>
        <ul class="list-group">
            <li class="list-group-item flex" v-for="(v,i) in cache_list">
               <div class="flex1" @click="open(v)"><b>{{i+1}}. </b>{{v.title}}</div>
               <small class="mr5">{{v.time}}</small>
               <span class="badge" @click="del(i)">删除</span>
            </li>
        </ul>
        <div class="center mb30">
            <button class="btn btn-default" @click="show_modal=false">关闭</button>
        </div>
    </div>
</div>
</body>
</html>
<script src="./assets/fun.js"></script>
<script src="./math-simple.js"></script>
<script>
/*
四年级暑假作业
4道竖式（小数的加减乘)，加减法：最多2位小数，乘法：最多三位有效数字，
2道递等式计算（包含简便运算）

*/
new Vue({
    el:"#app",
    data:{
        list:[],
        show:false,
        cache_list:[],
        show_modal:false,
    },
    created(){
        console.log(new Date().toLocaleString());
        this.cache_list=fun.cache("math4-2")||[];
        this.init();
    },
    methods:{
        init(){
            let list=this.float_plus(2);
            list=list.concat(this.float_mul(2));
            let arr=mathSimple.run(2);
            list=list.concat(arr);
            this.list=list;
            this.cache_list.push({
                time:fun.date(),
                list:this.list,
                title:this.list[0].exp.join(""),
            })
            fun.cache("math4-2",this.cache_list);
        },
        open(v){
            this.list=v.list;
            this.show_modal=false;
        },
        del(i){
            this.cache_list.splice(i,1);
            fun.cache("math4-2",this.cache_list);
            if(this.cache_list.length<1){
                this.show_modal=false;
            }
        },
        //两位小数的加减法
        float_plus(num){
            let list=[];
            for(let i=0;i<num;i++){
                let a=fun.rand(100,9999);
                let b=fun.rand(100,9999);
                let f=Math.random()>0.5?"+":"-";
                if(f=="-"&&a<b){
                    let c=a;
                    a=b;
                    b=c;
                }
                let r=(f=="+"?a+b:a-b)/100;
                list.push({
                    exp:[a/100,f,b/100],
                    r,
                })
            }
            return list;
        },
        //小数乘法
        float_mul(num){
            let list=[];
            for(let i=0;i<num;i++){
                let a=fun.rand(1,199)/this.rand_ten();
                let b=fun.rand(1,199)/this.rand_ten();
                let r=(Math.round(a*1000)*Math.round(b*1000))/1000000;
                list.push({
                    exp:[a,"×",b],
                    r,
                })
            }
            return list;
        },
        rand_ten(){
            let i=fun.rand(1,3);
            return Math.pow(10,i);
        },
        is_sign(v){
            let sign=["÷","×","+","-"];
            return sign.indexOf(v)>-1;
        },
        three_mul_two(len=2){
            let list=[];
            for(let i=0;i<len;i++){
                let a=fun.rand(100,999);
                let b=fun.rand(10,99);
                let r=a*b;
                list.push({
                    exp:[a,"×",b],
                    r,
                })
            }
            return list;
        },
        three_div_two(len=2){
            let list=[];
            for(let i=0;i<len;i++){
                let a=fun.rand(100,999);
                let b=fun.rand(10,99);
                let r=Math.floor(a/b);
                let ys=a%b;
                if(ys>0){
                    r=r+"......"+ys;
                }
                list.push({
                    exp:[a,"÷",b],
                    r,
                })
            }
            return list;
        }
    }
})
</script>